package com.link.ppt.View;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baoyz.actionsheet.ActionSheet;
import com.link.ppt.App.App;
import com.link.ppt.Base.BaseActivity;
import com.link.ppt.Model.Bean.BaseBean;
import com.link.ppt.Model.Bean.SimpleUserInfoBean;
import com.link.ppt.Model.IUserModel;
import com.link.ppt.Model.Impl.UserModelImpl;
import com.link.ppt.R;
import com.link.ppt.Utils.CommonSubscriber;
import com.link.ppt.Utils.PreferenceUtils;
import com.link.ppt.Utils.RxCountDown;
import com.link.ppt.Utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.Subscription;
import rx.functions.Action0;

/**
 * 登录画面
 * Created by liucxu on 2018/1/23.
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.edt_input_phone)
    EditText edt_input_phone;
    @BindView(R.id.edt_input_verification_code)
    EditText edt_input_verification_code;
    @BindView(R.id.layout_get_verification_code)
    RelativeLayout layout_get_verification_code;
    @BindView(R.id.tv_login)
    TextView tv_login;
    @BindView(R.id.tv_get_verification_code)
    TextView tv_get_verification_code;
    @BindView(R.id.tv_select_country_code)
    TextView tv_select_country_code;

    private IUserModel userModel;
    private Subscription subscription;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
    }

    @Override
    public void setupData() {
        userModel = new UserModelImpl();
    }

    @OnClick({R.id.layout_get_verification_code, R.id.tv_login, R.id.layout_select_country_code, R.id.layout_back})
    void doClicks(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.layout_get_verification_code:
                if (TextUtils.isEmpty(edt_input_phone.getText().toString())) {
                    ToastUtils.showShortToast(LoginActivity.this, getResources().getString(R.string.ppt_string_11));
                } else {
                    userModel.GetVerificationCode(edt_input_phone.getText().toString(), tv_select_country_code.getText().toString()).doOnSubscribe(new Action0() {
                        @Override
                        public void call() {
                            showProgress();
                            layout_get_verification_code.setEnabled(false);
                        }
                    }).subscribe(new CommonSubscriber<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean response) {
                            hideProgress();
                            subscription = RxCountDown.countdown(60).subscribe(new Observer<Integer>() {
                                @Override
                                public void onCompleted() {
                                    layout_get_verification_code.setEnabled(true);
                                    tv_get_verification_code.setText(getResources().getString(R.string.ppt_string_3));
                                }

                                @Override
                                public void onError(Throwable e) {
                                    layout_get_verification_code.setEnabled(true);
                                }

                                @Override
                                public void onNext(Integer integer) {
                                    layout_get_verification_code.setEnabled(false);
                                    tv_get_verification_code.setText(integer + "S");
                                }
                            });
                        }

                        @Override
                        public void onFailure(BaseBean response) {
                            hideProgress();
                            layout_get_verification_code.setEnabled(true);
                            ToastUtils.showShortToast(LoginActivity.this, response.getDesc());
                        }
                    });
                }
                break;
            case R.id.tv_login:
                login();
                break;
            case R.id.layout_select_country_code:
                ActionSheet.createBuilder(this, getSupportFragmentManager())
                        .setCancelButtonTitle(getResources().getString(R.string.ppt_string_13))
                        .setOtherButtonTitles("+86", "+001", "+852")
                        .setCancelableOnTouchOutside(true)
                        .setListener(new ActionSheet.ActionSheetListener() {
                            @Override
                            public void onDismiss(ActionSheet actionSheet, boolean isCancel) {

                            }

                            @Override
                            public void onOtherButtonClick(ActionSheet actionSheet, int index) {
                                if (index == 0) {
                                    tv_select_country_code.setText("+86");
                                }
                                if (index == 1) {
                                    tv_select_country_code.setText("+001");
                                }
                                if (index == 2) {
                                    tv_select_country_code.setText("+852");
                                }
                            }
                        }).show();
                break;
            case R.id.layout_back:
                App.GetInstance().finishActivity(this);
                break;
        }
    }

    private void login() {
        if (TextUtils.isEmpty(edt_input_phone.getText().toString())) {
            ToastUtils.showShortToast(LoginActivity.this, getResources().getString(R.string.ppt_string_11));
            return;
        }
        if (TextUtils.isEmpty(edt_input_verification_code.getText().toString())) {
            ToastUtils.showShortToast(LoginActivity.this, getResources().getString(R.string.ppt_string_12));
            return;
        }
        userModel.LoginByCode(edt_input_phone.getText().toString(), edt_input_verification_code.getText().toString()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showProgress();
            }
        }).subscribe(new CommonSubscriber<SimpleUserInfoBean>() {
            @Override
            public void onSuccess(SimpleUserInfoBean response) {
                hideProgress();
                PreferenceUtils.SaveCountryCode(tv_select_country_code.getText().toString());
                PreferenceUtils.SaveUserId(response.getUserId());
                PreferenceUtils.SaveUserToken(response.getDesc());
                App.GetInstance().finishActivity(LoginActivity.this);
            }

            @Override
            public void onFailure(BaseBean response) {
                hideProgress();
                //未注册
                if (response.getCode() == 2) {
                    PreferenceUtils.SaveCountryCode(tv_select_country_code.getText().toString());
                    Intent intent = new Intent(LoginActivity.this, CompleteUserInfoActivity.class);
                    intent.putExtra("mobile", edt_input_phone.getText().toString());
                    intent.putExtra("code", edt_input_verification_code.getText().toString());
                    startActivity(intent);
                } else {
                    ToastUtils.showShortToast(LoginActivity.this, response.getDesc());
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (subscription != null) {
            subscription.unsubscribe();
        }
        super.onDestroy();
    }
}
